//
//  MainMenuLayer.m
//  SpaceViking
//
//  Created by gnt on 1/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainMenuLayer.h"
#import "DemoMainMenu.h"
#import "DemoMainMenu2.h"

@implementation MainMenuLayer

- (void)playScene: (CCMenuItemFont*)itemPassedIn {
    if ([itemPassedIn tag] == 1) {
        CCLOG(@"Tag 1 found, Scene 1");
        [[GameManager sharedGameManager] runSceneWithID:kGameLevel1];
    } 
    else if([itemPassedIn tag] == 2) {
        [[GameManager sharedGameManager] runSceneWithID:kGameLevel2];
    }
    else {
        CCLOG(@"Tag was: %d", [itemPassedIn tag]);
        CCLOG(@"Placeholder for next chapters");
    }
}

-(void)displayMainMenu {
    CGSize screenSize = [CCDirector sharedDirector].winSize; 
    if (sceneSelectMenu != nil) {
        [sceneSelectMenu removeFromParentAndCleanup:YES];
    }
    // Main Menu
    CCMenuItemImage *playGameButton = [CCMenuItemImage 
                                       itemFromNormalImage:@"PlayGameButtonNormal.png" 
                                       selectedImage:@"PlayGameButtonSelected.png" 
                                       disabledImage:nil 
                                       target:self 
                                       selector:@selector(displaySceneSelection)];
    
//    CCMenuItemImage *buyBookButton = [CCMenuItemImage 
//                                      itemFromNormalImage:@"BuyBookButtonNormal.png" 
//                                      selectedImage:@"BuyBookButtonSelected.png" 
//                                      disabledImage:nil 
//                                      target:self 
//                                      selector:@selector(buyBook)];
    
//    CCMenuItemImage *optionsButton = [CCMenuItemImage 
//                                      itemFromNormalImage:@"OptionsButtonNormal.png" 
//                                      selectedImage:@"OptionsButtonSelected.png" 
//                                      disabledImage:nil 
//                                      target:self 
//                                      selector:@selector(showOptions)];
//    
    mainMenu = [CCMenu menuWithItems:playGameButton,nil];
    [mainMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
    [mainMenu setPosition: ccp(screenSize.width * 2.0f, screenSize.height / 2.0f)];
    id moveAction = [CCMoveTo actionWithDuration:1.2f 
                        position:ccp(screenSize.width * 0.85f, screenSize.height/2.0f)];
    id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
    [mainMenu runAction:moveEffect];
    [self addChild:mainMenu z:0 tag:10];
}

- (void)displayView {
    // get the cocos2d view (it's the EAGLView class which inherits from UIView)
	UIView* glView = [CCDirector sharedDirector].openGLView;
	// The dummy UIView you created in the App delegate is the superview of the glView
	UIView* dummyView = glView.superview;
    
    // regular text field with rounded corners
	UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 20, 200, 24)];
	textField.text = @"  Regular UITextField";
	textField.borderStyle = UITextBorderStyleRoundedRect;
	textField.delegate = self;
    [dummyView addSubview:textField];
    [textField release];
    // send the cocos2d view to the front so it is in front of the other views
	//[dummyView bringSubviewToFront:glView];
    
	
	// make the cocos2d view transparent
	// IMPORTANT: transparent cocos2d view requires EAGLView pixelFormat set to kEAGLColorFormatRGBA8 (not the default)
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glView.opaque = NO;
    
    
    // add a Interface Builder view
	DemoMainMenu2* myViewController = [[DemoMainMenu2 alloc] initWithNibName:@"DemoMainMenu2" bundle:nil];
//    DemoMainMenu* myViewController = [[DemoMainMenu alloc]init];
	[dummyView addSubview:myViewController.view];
//	[dummyView sendSubviewToBack:myViewController.view]; // optional
	[myViewController release];
}

- (void)displaySceneSelection {
    CGSize screenSize = [CCDirector sharedDirector].winSize; 
    if (mainMenu != nil) {
        [mainMenu removeFromParentAndCleanup:YES];
    }
    
    CCLabelBMFont *playScene1Label = [CCLabelBMFont labelWithString:@"SCENE 1" 
                                                            fntFile:@"SpaceVikingFont.fnt"];
    CCMenuItemLabel *playScene1 = [CCMenuItemLabel itemWithLabel:playScene1Label target:self 
                                                        selector:@selector(playScene:)];
    [playScene1 setTag:1];
    
    CCLabelBMFont *playScene2Label = [CCLabelBMFont labelWithString:@"SCENE 2" 
                                                            fntFile:@"SpaceVikingFont.fnt"];
    CCMenuItemLabel *playScene2 = [CCMenuItemLabel itemWithLabel:playScene2Label target:self 
                                                        selector:@selector(playScene:)];
    [playScene2 setTag:2];
    
    
    CCLabelBMFont *backButtonLabel = [CCLabelBMFont labelWithString:@"Back" 
                                                            fntFile:@"SpaceVikingFont.fnt"];
    CCMenuItemLabel *backButton = [CCMenuItemLabel itemWithLabel:backButtonLabel target:self 
                                                        selector:@selector(displayMainMenu)];
    
    sceneSelectMenu = [CCMenu menuWithItems:playScene1, playScene2, backButton, nil];
    [sceneSelectMenu alignItemsVerticallyWithPadding:screenSize.height * 0.059f];
    [sceneSelectMenu setPosition:ccp(screenSize.width * 2, screenSize.height / 2)];
    
    id moveAction = [CCMoveTo actionWithDuration:0.5f 
                                        position:ccp(screenSize.width * 0.75f, screenSize.height/2)];
    id moveEffect = [CCEaseIn actionWithAction:moveAction rate:1.0f];
    [sceneSelectMenu runAction:moveEffect];
    [self addChild:sceneSelectMenu z:1 tag:11];
}

- (id)init {
    self = [super init];
    if (self != nil) {
        CGSize screenSize = [CCDirector sharedDirector].winSize; 
        
//        CCSprite *background = [CCSprite spriteWithFile:@"bg2.png"];
//        [background setPosition:ccp(screenSize.width/2, screenSize.height/2)];
//        [self addChild:background];
        [self displayMainMenu];
        [self displayView];
    }
    return self;
}
@end
